package com.cskaoyan.mall.controller.admin;

import com.cskaoyan.mall.model.*;
import com.cskaoyan.mall.model.bo.GoodsParamBO;
import com.cskaoyan.mall.model.bo.ReplyBO;
import com.cskaoyan.mall.model.bo.SpecBO;
import com.cskaoyan.mall.model.vo.GoodsVO;
import com.cskaoyan.mall.model.vo.NoReplyMsgVO;
import com.cskaoyan.mall.model.vo.RepliedMsgVO;
import com.cskaoyan.mall.service.GoodsService;
import com.cskaoyan.mall.service.GoodsServiceImpl;
import com.cskaoyan.mall.utils.FileUploadUtils;
import com.cskaoyan.mall.utils.HttpUtils;
import com.google.gson.Gson;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * User: zsquirrel
 * Date: 2020/4/30
 * Time: 9:44 上午
 */
@WebServlet("/api/admin/goods/*")
public class GoodsServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImpl();

    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        String action = requestURI.replace("/api/admin/goods/", "");
        if("imgUpload".equals(action)){
            imgUpload(request, response);
        }else if("addGoods".equals(action)){
            addGoods(request, response);
        }else if("deleteSpec".equals(action)){
            deleteSpec(request, response);
        }else if("addSpec".equals(action)){
            addSpec(request, response);
        }else if("updateGoods".equals(action)){
            updateGoods(request, response);
        }else if("reply".equals(action)){
            reply(request, response);
        }
    }

    private void reply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        ReplyBO replyBO = gson.fromJson(requestBody, ReplyBO.class);
        goodsService.reply(replyBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    private void updateGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsParamBO paramBO = gson.fromJson(requestBody, GoodsParamBO.class);
        goodsService.updateGoods(paramBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 这一步不需要将spec保存到数据库，反而后续操作麻烦
     * @param request
     * @param response
     * @throws IOException
     */
    private void addSpec(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Spec spec = gson.fromJson(requestBody, Spec.class);
        //goodsService.addSpec(spec);
        response.getWriter().println(gson.toJson(Result.ok(spec)));
    }

    private void deleteSpec(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        SpecBO specBO = gson.fromJson(requestBody, SpecBO.class);
        goodsService.deleteSpec(specBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    private void addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsParamBO goodsParamBO = gson.fromJson(requestBody, GoodsParamBO.class);
        goodsService.addGoods(goodsParamBO);
        Result result = new Result();
        result.setCode(0);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 单独的文件上传，里面不会含有普通的form表单数据
     * 传输的文件二进制数据就在请求体里面
     * 但是里面有分隔符WebKitFormBounday
     * @param request
     * @param response
     */
    private void imgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //为了稳妥起见，可以先校验一下是否含有multipartContent
        boolean content = ServletFileUpload.isMultipartContent(request);
        if(!content){
            //return 10000 message
        }
        Map<String, Object> map = FileUploadUtils.parseRequest(request);
        System.out.println(map);
        //细节点  /upload/......
        String domain = (String) getServletContext().getAttribute("domain");
        String file = (String) map.get("file");
        file = domain + file;
        System.out.println(file);
        Result result = new Result();
        result.setCode(0);
        result.setData(file);
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        String action = requestURI.replace("/api/admin/goods/", "");
        if("getType".equals(action)){
            getType(request, response);
        }else if("getGoodsByType".equals(action)){
            getGoodsByType(request, response);
        }else if("deleteType".equals(action)){
            deleteType(request, response);
        }else if("getGoodsInfo".equals(action)){
            getGoodsInfo(request, response);
        }else if("deleteGoods".equals(action)){
            deleteGoods(request, response);
        }else if("noReplyMsg".equals(action)){
            noReplyMsg(request, response);
        }else if("repliedMsg".equals(action)){
            repliedMsg(request, response);
        }
    }

    private void repliedMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<RepliedMsgVO> repliedMsgVOS =  goodsService.repliedMsg();
        response.getWriter().println(gson.toJson(Result.ok(repliedMsgVOS)));
    }

    private void noReplyMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<NoReplyMsgVO> noReplyMsgVOS = goodsService.noReplyMsg();
        response.getWriter().println(gson.toJson(Result.ok(noReplyMsgVOS)));
    }

    /**
     * 删除商品记得把商品相关问答，评价删除
     * @param request
     * @param response
     * @throws IOException
     */
    private void deleteGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        goodsService.deleteGoods(id);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        GoodsVO goodsVO = goodsService.getGoodsInfo(id);
        response.getWriter().println(gson.toJson(Result.ok(goodsVO)));
    }

    private void deleteType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        List<Goods> goodsList = goodsService.getGoodsByType(typeId);
        if(goodsList == null || goodsList.size() == 0){
            goodsService.deleteType(typeId);
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("当前分类下尚有商品，无法删除")));
    }

    /**
     * 根据相应的分类查询当前分类下的商品
     * @param request
     * @param response
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        //先来一些校验
        List<Goods> goodsList = goodsService.getGoodsByType(typeId);
        Result result = new Result();
        result.setCode(0);
        result.setData(goodsList);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 查询商品之前首先要查询商品的分类
     * @param request
     * @param response
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> typeList = goodsService.getType();
        Result result = new Result();
        result.setCode(0);
        result.setData(typeList);
        response.getWriter().println(gson.toJson(result));
    }
}
